package com.dog.basics.netty.kit;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dog.basics.bean.model.mtkj.MtDeviceLock;
import com.dog.basics.common.util.SpringContextUtils;
import com.dog.basics.netty.util.SessionSocketHolder;
import com.dog.basics.service.mtkj.IMtDeviceLockService;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 *  设备强制下线
 */
@Component
@Log4j2
public class RouteHandler {

    /**
     * 用户下线
     * @param sessionChannelId
     * @param channel
     * @throws IOException
     */
    public void userOffLine(Long sessionChannelId , NioSocketChannel channel) throws IOException {
		log.info("用户强制 : [{}]下线", sessionChannelId);
		if (ObjectUtil.isNotNull(sessionChannelId)){
			SessionSocketHolder.logu(sessionChannelId);
			// 强制设备下线
			MtDeviceLock deviceLock = new MtDeviceLock();
			deviceLock.setId(sessionChannelId);
			deviceLock.setDevOnlineState(0);
			deviceLock.setUpdateTime(DateUtil.currentSeconds());
			SpringContextUtils.getBean(IMtDeviceLockService.class).updateById(deviceLock);
		}else {
			SessionSocketHolder.logu(channel.id().asLongText());
		}
    }

}
